package nz.ac.massey.cs.compile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import nz.ac.massey.cs.domain.Cell;
import nz.ac.massey.cs.domain.IConstraint;

import org.java.plugin.Plugin;

public abstract class AbstractConstraint extends Plugin implements IConstraint {
		protected ArrayList<Cell> cells;
		protected String type = "unique";
		protected Map<String, String> drawingInfo;
		protected ArrayList<String> Range = new ArrayList<String>();
		protected Map<String,String> constraintInfo; // not currently used
		
		public Map<String, String> getAllConstraintInfo() {
			return constraintInfo;
		}

		public void setConstraintInfo(Map<String, String> constraintInfo) {
			this.constraintInfo = constraintInfo;
		}
		
		public String getConstraintInfo(String key)
		{
			return this.constraintInfo.get(key);
		}

		/* (non-Javadoc)
		 * @see tims.domain.IConstraint#addCell(tims.domain.Cell)
		 */
		@Override
		public void addCell(Cell cellToAdd)
		{
			cells.add(cellToAdd);
		}
		
		/* (non-Javadoc)
		 * @see tims.domain.IConstraint#isViolated()
		 */
		@Override
		public abstract boolean isViolated();
//		{
//			for(Cell a:cells)
//			{
//				String ValueA = a.getValue(); 
//				if(!ValueA.equals(""))
//				{
//					for(Cell b:cells)
//					{
//						String ValueB = b.getValue();
//						if(!ValueB.equals("") && a != b)
//						{
//							if(ValueA.equals(ValueB) || !Range.contains(ValueA) || !Range.contains(ValueB))
//							{
//								return true;
//							}
//						}
//					}
//				}
//			}
//			return false;
//		}
		
		/* (non-Javadoc)
		 * @see tims.domain.IConstraint#isSatisfied()
		 */
		@Override
		public boolean isSatisfied()
		{
			for(Cell a:cells)
			{
				if(a.getValue().equals(""))
				{
					return false;
				}
			}
			return isViolated();
		}

		@Override
		public String getType() {
			return this.type;
		}

		@Override
		public Map<String, String> getDrawingInfo() {
			return drawingInfo; 
		}

		@Override
		public ArrayList<Cell> getCells() {
			return cells;
		}

		@Override
		public void setConstraintInfo(String constraintInfo, ArrayList<String> range) {
			this.cells = new ArrayList<Cell>();
			this.Range = range;
			this.drawingInfo = new HashMap<String, String>();
			applyConstraintInfo(constraintInfo);
		}
		
		public abstract void applyConstraintInfo(String constraintInfo);

		@Override
		protected void doStart() throws Exception {
			// TODO Auto-generated method stub
			
		}

		@Override
		protected void doStop() throws Exception {
			// TODO Auto-generated method stub
			
		}
}
